using ECETCentralController.Communication;
using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UIFramework;
using UnityEngine;
using UnityEngine.UI;

public class DevicePanel : BaseView
{
    public class DeviceContext : BaseContext
    {
        public DeviceContext():base(UIType.DevicePanel) { }
    }

    public Button backBtn;
    public Transform content;

    private string tcpIp = "192.168.10.100";
    private int tcpPort = 8989;
    public override void OnEnter(BaseContext context)
    {
        this.gameObject.SetActive(true);
        base.OnEnter(context);

        InitEvent();
        //InitData();
    }
    public override void OnExit(BaseContext context)
    {
        base.OnExit(context);
    }
    private void InitEvent()
    {
        backBtn.onClick.AddListener(() => {
            Singleton<ContextManager>.Instance.Pop();
            Singleton<ContextManager>.Instance.Push(new MainView.MainContext());
        });
    }

    public void SendTcpMsg(string msg)
    {
        TCPUtil.TcpClientThread.TcpSendMsg(tcpIp, tcpPort, msg.ToString().Trim());
    }

    private void InitData()
    {
        string url = HttpUtils.GetUrl(HttpUtils.deviceUrl);
        ClientManager.Instance.GetRequest(url, "", OnGetData);
    }

    private void OnGetData(string Info)
    {
        Debug.Log(Info);
        ResponseS2C<List<Equipment>> response = JsonMapper.ToObject<ResponseS2C<List<Equipment>>>(Info);
        if (response.code == 200)
        {
            var equipments = response.data;
            for (int i = 0;i< equipments.Count; i++)
            {
                if (i >= content.childCount -2) break;
                var child = content.GetChild(i + 1).gameObject;
                UpdateItemInfo(child, equipments[i]);
            }
        }
    }
    private void UpdateItemInfo(GameObject go,Equipment equipment)
    {
        var des = FindComponent<Text>("des",go);
        des.text = equipment.equipmentTitle;
    }


    public class Equipment
    {
        public int id;
        public int exhibitionHallId;
        public int exhibitionAreaId;
        public string equipmentTitle;
    }

    public void AllOn()
    {
        Light_On();
        Thread.Sleep(10);
        Gui_On();
        Thread.Sleep(10);
        Ru_Screen_On();
        Thread.Sleep(10);
        Yuan_Screen_On();
        Thread.Sleep(10);
        Quan_Screen_On();
        Thread.Sleep(10);
        Shi_On();
        Thread.Sleep(10);
        Ru_Pc_On();
        Thread.Sleep(10);
        Yuan_Pc_On();
        Thread.Sleep(10);
        Quan_Pc_On();                  
    }
    public void AllOff()
    {
        Ru_Pc_Off();
        Thread.Sleep(10);
        Yuan_Pc_Off();
        Thread.Sleep(10);
        Quan_Pc_Off();
        Thread.Sleep(10);
        Shi_Off();
        Thread.Sleep(10);
        Ru_Screen_Off();
        Thread.Sleep(10);
        Yuan_Screen_Off();
        Thread.Sleep(10);
        Quan_Screen_Off();
        Thread.Sleep(10);
        Gui_Off();
        Thread.Sleep(10);
        Light_Off(); 
    }

    public void Ru_Pc_On()
    {
        UDPSender.SendString(ru_pc_Ip,pc_Port,"start");
    }
    public void Ru_Pc_Off()
    {
        UDPSender.SendString(ru_pc_Ip, pc_Port, "stop");
    }
    public void Yuan_Pc_On()
    {
        UDPSender.SendString(yuan_pc_Ip, pc_Port, "start");
    }
    public void Yuan_Pc_Off()
    {
        UDPSender.SendString(yuan_pc_Ip, pc_Port, "stop");
    }
    public void Quan_Pc_On()
    {
        UDPSender.SendString(quan_pc_Ip, pc_Port, "start");
    }
    public void Quan_Pc_Off()
    {
        UDPSender.SendString(quan_pc_Ip, pc_Port, "stop");
    }
    public void Ru_Screen_On()
    {
        for (int i = 0; i < ru_screen_on.Length; i++)
        {
            UDPSender.SendBytes(ru_screen_Ip, screen_Port, StrToBytes(ru_screen_on[i]));
            Thread.Sleep(1000);
        }
    }
    public void Ru_Screen_Off()
    {
        for (int i = 0; i < ru_screen_off.Length; i++)
        {
            UDPSender.SendBytes(ru_screen_Ip, screen_Port, StrToBytes(ru_screen_off[i]));
            Thread.Sleep(1000);
        }
    }
    public void Yuan_Screen_On()
    {
        for (int i = 0; i < yuan_screen_on.Length; i++)
        {
            UDPSender.SendBytes(yuan_screen_Ip, screen_Port, StrToBytes(yuan_screen_on[i]));
            Thread.Sleep(1000);
        }
    }
    public void Yuan_Screen_Off()
    {
        for (int i = 0; i < yuan_screen_off.Length; i++)
        {
            UDPSender.SendBytes(yuan_screen_Ip, screen_Port, StrToBytes(yuan_screen_off[i]));
            Thread.Sleep(1000);
        }
    }
    public void Quan_Screen_On()
    {
        for (int i = 0; i < quan_screen_on.Length; i++)
        {
            UDPSender.SendBytes(quan_screen_Ip, screen_Port, StrToBytes(quan_screen_on[i]));
            Thread.Sleep(1000);
        }
    }
    public void Quan_Screen_Off()
    {
        for (int i = 0; i < quan_screen_off.Length; i++)
        {
            UDPSender.SendBytes(quan_screen_Ip, screen_Port, StrToBytes(quan_screen_off[i]));
            Thread.Sleep(1000);
        }
    }
    public void Shi_On()
    {
        UDPSender.SendBytes(shi_Ip, shi1_port, StrToBytes(shi1_powerOn));
        Thread.Sleep(10);
        UDPSender.SendBytes(shi_Ip, shi2_port, StrToBytes(shi2_powerOn));
    }
    public void Shi_Off()
    {
        UDPSender.SendBytes(shi_Ip, shi1_port, StrToBytes(shi1_powerOff));
        Thread.Sleep(10);
        UDPSender.SendBytes(shi_Ip, shi2_port, StrToBytes(shi2_powerOff));
    }
    public void Gui_On()
    {
        UDPSender.SendBytes(gui1_Ip, port, StrToBytes(gui1_on));
        Thread.Sleep(10);
        for (int i = 0; i < gui2_on.Length; i++)
        {
            UDPSender.SendBytes(gui2_Ip, port, StrToBytes(gui2_on[i]));
            Thread.Sleep(1000);
        }
        Thread.Sleep(1000);
        for (int i = 0; i < gui3_on.Length; i++)
        {
            UDPSender.SendBytes(gui3_Ip, port, StrToBytes(gui3_on[i]));
            Thread.Sleep(1000);
        }
    }
    public void Gui_Off()
    {
        UDPSender.SendBytes(gui1_Ip, port, StrToBytes(gui1_off));
        Thread.Sleep(10);
        for (int i = 0; i < gui2_off.Length; i++)
        {
            UDPSender.SendBytes(gui2_Ip, port, StrToBytes(gui2_off[i]));
            Thread.Sleep(1000);
        }
        Thread.Sleep(1000);
        for (int i = 0; i < gui3_off.Length; i++)
        {
            UDPSender.SendBytes(gui3_Ip, port, StrToBytes(gui3_off[i]));
            Thread.Sleep(1000);
        }
    }
    public void Light_On()
    {
        for (int i = 0; i < light1_on.Length; i++)
        {
            UDPSender.SendBytes(gui1_Ip, port, StrToBytes(light1_on[i]));
            Thread.Sleep(1000);
        }
        Thread.Sleep(10); 
        for (int i = 0; i < light2_on.Length; i++)
        {
            UDPSender.SendBytes(gui2_Ip, port, StrToBytes(light2_on[i]));
            Thread.Sleep(1000);
        }
        Thread.Sleep(1000);
        for (int i = 0; i < light3_on.Length; i++)
        {
            UDPSender.SendBytes(gui3_Ip, port, StrToBytes(light3_on[i]));
            Thread.Sleep(1000);
        }
        Thread.Sleep(10);
        for (int i = 0; i < light4_on.Length; i++)
        {
            UDPSender.SendBytes(gui4_Ip, port, StrToBytes(light4_on[i]));
            Thread.Sleep(1000);
        }
    }
    public void Light_Off()
    {
        for (int i = 0; i < light1_off.Length; i++)
        {
            UDPSender.SendBytes(gui1_Ip, port, StrToBytes(light1_off[i]));
            Thread.Sleep(1000);
        }
        Thread.Sleep(10);
        for (int i = 0; i < light2_off.Length; i++)
        {
            UDPSender.SendBytes(gui2_Ip, port, StrToBytes(light2_off[i]));
            Thread.Sleep(1000);
        }
        Thread.Sleep(1000);
        for (int i = 0; i < light3_off.Length; i++)
        {
            UDPSender.SendBytes(gui3_Ip, port, StrToBytes(light3_off[i]));
            Thread.Sleep(1000);
        }
        Thread.Sleep(10);
        for (int i = 0; i < light4_off.Length; i++)
        {
            UDPSender.SendBytes(gui4_Ip, port, StrToBytes(light4_off[i]));
            Thread.Sleep(1000);
        }
    }

    private string ru_pc_Ip = "192.168.10.51";
    private string yuan_pc_Ip = "192.168.10.52";
    private string quan_pc_Ip = "192.168.10.53";
    private int pc_Port = 7001;

    private string ru_screen_Ip = "192.168.10.110";
    private string yuan_screen_Ip = "192.168.10.111";
    private string quan_screen_Ip = "192.168.10.112";
    private int screen_Port = 8100;

    private string shi_Ip = "192.168.10.105";
    private int shi1_port = 5001;
    private int shi2_port = 5002;

    private string gui1_Ip = "192.168.10.106";
    private string gui2_Ip = "192.168.10.107";
    private string gui3_Ip = "192.168.10.108";
    private string gui4_Ip = "192.168.10.109";
    private int port = 7002;

    public byte[] StrToBytes(string hexString)
    {
        hexString = hexString.Replace(" ", "");
        if ((hexString.Length % 2) != 0)
            hexString += " ";
        byte[] returnBytes = new byte[hexString.Length / 2];
        for (int i = 0; i < returnBytes.Length; i++)
        {
            returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
        }
        return returnBytes;
    }

    private string[] ru_screen_on = new string[]
    {
        "14 00 06 00 00 00 00 00 00 00 b0 6d",
        "14 00 06 00 01 00 00 00 00 00 b1 bc"
    };
    private string[] ru_screen_off = new string[]
    {
        "14 00 06 00 00 01 00 00 00 00 8d ad",
        "14 00 06 00 01 01 00 00 00 00 8c 7c"
    };

    private string[] yuan_screen_on = new string[] 
    {
        "14 00 06 00 00 00 00 00 00 00 b0 6d",
        "14 00 06 00 01 00 00 00 00 00 b1 bc"
    };
    private string[] yuan_screen_off = new string[]
    {
        "14 00 06 00 00 01 00 00 00 00 8d ad",
        "14 00 06 00 01 01 00 00 00 00 8c 7c"
    };
    private string[] quan_screen_on = new string[]
    {
        "14 00 06 00 00 00 00 00 00 00 b0 6d",
        "14 00 06 00 01 00 00 00 00 00 b1 bc"
    };
    private string[] quan_screen_off = new string[]
    {
        "14 00 06 00 00 01 00 00 00 00 8d ad",
        "14 00 06 00 01 01 00 00 00 00 8c 7c"
    };
    private string shi1_powerOn = "FE 01 00 02 01 FF";
    private string shi1_powerOff = "FE 01 00 02 00 FF";

    private string shi2_powerOn = "FE 01 00 02 01 FF";
    private string shi2_powerOff = "FE 01 00 02 00 FF";

    private string gui1_on = "AA 0F 05 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB";
    private string gui1_off = "AA 0F 05 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB";

    private string[] gui2_on = new string[]
    {
        "AA 0F 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 02 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 04 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 05 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 06 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB"
    };
    private string[] gui2_off = new string[]
    {
        "AA 0F 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 01 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 02 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 04 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 05 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 06 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB"
    };

    private string[] gui3_on = new string[]
    {
        "AA 0F 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 05 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 07 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB"
    };
    private string[] gui3_off = new string[]
    {
        "AA 0F 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 05 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 07 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB"
    };

    private string[] light1_on = new string[]
    {
        "AA 0F 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 02 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 03 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 04 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 06 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 07 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB"
    }; 
    private string[] light1_off = new string[]
    {
        "AA 0F 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 01 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 02 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 03 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 04 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 06 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 07 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB"
    };
    private string[] light2_on = new string[]
    {
        "AA 0F 03 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 07 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB"
    };
    private string[] light2_off = new string[]
    {
         "AA 0F 03 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 07 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB"
    };
    private string[] light3_on = new string[]
    {
        "AA 0F 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 02 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 03 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 04 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 06 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB"
    };
    private string[] light3_off = new string[]
   {
        "AA 0F 01 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 02 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 03 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 04 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 06 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB"
   };
    private string[] light4_on = new string[]
    {
        "AA 0F 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB"
    }; 
    private string[] light4_off = new string[]
    {
        "AA 0F 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB",
        "AA 0F 01 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 BB"
    };
}
